﻿#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportStore.Models.Abstract;
using SportStore.Models.Concrete;
using SportStore.Models;
using SportStore.Models.Entities;
using System.IO;
using SportStore.Framework;
using System.Threading;
#endregion

namespace SportStore.Controllers
{
    public class ProductsController : Controller
    {
        private IProductRepository repository = new EFProductRepository();

        public int PageSize = 4;


        #region GetImage
        public string GetImage(int id,bool thumb)
        {
            Product prod = repository.Products.FirstOrDefault(p => p.ProductID == id);
            return thumb?prod.ImageFileName + "thumb":prod.ImageFileName;
        }
        #endregion

        #region List
        public ActionResult List(string category, int page = 1)
        {
            if (category == "null") category = null;
            int itemsPerPage = int.Parse(Request.Cookies["itemsPerPage"].Value.ToString());
            var totalItems = repository.Products.Where(p => category == null || p.Category.Name == category).OrderBy(p => p.ProductID);
            var currentItems = repository.Products.Where(p => category == null || p.Category.Name == category).OrderBy(p => p.ProductID).Skip((page - 1) * PageSize).Take(itemsPerPage);
            ProductsListViewModel viewModel = new ProductsListViewModel
            {
                Products = currentItems,
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = itemsPerPage, 
                    TotalItems = totalItems.Count()
                },
                Category = category
            };
            return View(viewModel);
        }
        #endregion

    }
}
